package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.common.enums.OrdersStatusEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Orders;
import com.example.entity.Tourism;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.OrdersMapper;
import com.example.mapper.TourismMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private TourismMapper tourismMapper;

    /**
     * 新增
     */
    @Transactional     //todo 笔记：下单逻辑涉及到订单表和商品表，需要保证数据一致性，故加上事务注解
    public void add(Orders orders) {
        //下单之前需判断是否还有库存 （todo 笔记：应该先判断是否还有库存，有库存再设置其他值）
        Integer tourismId = orders.getTourismId(); //旅游商品id
        Tourism tourism = tourismMapper.selectById(tourismId);
        if (tourism == null) {
            throw new CustomException("500","商品不存在");
        }
        if (tourism.getStore() < orders.getNum()) { //商品库存不足
            throw new CustomException("500","库存不足");
        }

        Account currentUser = TokenUtils.getCurrentUser();
        orders.setUserId(currentUser.getId());
        orders.setTime(DateUtil.now());
        orders.setStatus(OrdersStatusEnum.WAIT_PAY.getDesc()); //设置订单状态为待支付
        orders.setOrderNo(IdUtil.getSnowflakeNextIdStr()); //随机生成订单编号
        ordersMapper.insert(orders); //下单

        //下单之后，更新库存
        tourism.setStore(tourism.getStore() - orders.getNum());
        tourismMapper.updateById(tourism);
    }

    /**
     * 更新
     */
    public void updateById(Orders orders) {
        ordersMapper.updateById(orders);
    }

    /**
     * 删除
     */
    public void deleteByid(Integer id) {
        ordersMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
/*        for (Integer id : ids) {
            ordersMapper.deleteById(id);
        }*/
        ordersMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询
     */
    public Orders selectById(Integer id) {
        return ordersMapper.selectById(id);
    }

    /**
     * 查询所有（条件查询）
     */
    public List<Orders> selectAll(Orders orders) {
        return ordersMapper.selectAll(orders);
    }

    /**
     * 分页查询（在查询所有的基础上进行分页）
     */
    public PageInfo<Orders> selectPage(Orders orders,Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser.getRole().equals(RoleEnum.USER.name())) {
            orders.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> list = ordersMapper.selectAll(orders);
        return PageInfo.of(list);
    }

    /**
     * 根据订单号查询订单信息
     */
    public Orders selectByOrderNo(String orderNo) {
        return ordersMapper.selectByOrderNo(orderNo);
    }
}
